package com.zhl.dao.dasedao;

import com.zhl.annotions.myField;
import com.zhl.annotions.myTable;
import com.zhl.utils.JdbcUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Arrays;

public class BaseDao<T> {

    private Class<T> tClass;
    private String tableName;
    private String keyName;

    JdbcTemplate jdbcTemplate = new JdbcTemplate(JdbcUtils.getDataSource());

    //获取当前数据库名,类名,键值
    public BaseDao(Class<T> tClass) {
        this.tClass = tClass;
        myTable anno = tClass.getAnnotation(myTable.class);
        if (anno != null){
            tableName = anno.value();
            keyName = anno.keyName();
        }
    }

    //添加记录
    public boolean Save(T t){
        int set = 0;
        Class<?> aClass = t.getClass();
        Field[] fields = aClass.getDeclaredFields();
        int length = fields.length;
        try {
            StringBuilder sql = new StringBuilder();
            sql.append("insert into ").append(tableName).append("(");
            for (int i = 0; i < length; i++) {
                String name = fields[i].getName();
                myField anno = fields[i].getAnnotation(myField.class);
                if (anno != null){
                    name = anno.field();
                }
                if (i == length - 1){
                    sql.append("`").append(name).append("`)");
                }else {
                    sql.append("`").append(name).append("`,");
                }
            }
            sql.append("values(");
            for (int i = 0; i < length; i++) {
                if (i == length - 1){
                    sql.append("?)");
                }
                else {
                    sql.append("?,");
                }
            }
            Object[] objects = new Object[length];
            for (int i = 0; i < length; i++) {
                String name = fields[i].getName();
                String getMethodName = "get" + name.substring(0,1).toUpperCase() + name.substring(1);
                Method method = tClass.getDeclaredMethod(getMethodName);
                Object obj = method.invoke(t);
                objects[i] = obj;
            }
            set = jdbcTemplate.update(sql.toString(),objects);
        }catch (Exception e){
            e.printStackTrace();
        }
        return set > 0;
    }

/*    //删除指定ID记录
    public boolean Delete(Object id){
        int a = 0;
        try {
            String sql = "delete from " + tableName + " where "+ keyName +" = ?";
            a = jdbcTemplate.update(sql,id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return a > 0;
    }*/

    //修改记录
    public boolean UpdateById(T t,Object id){
        int a = 0;
        Class<?> aClass = t.getClass();
        Field[] fields = aClass.getDeclaredFields();
        int length = fields.length;
        //sql = "update tb_student set stu_Id = ?,stu_Name = ?,stu_Age = ?,stu_Address = ? where stu_Id = ?";
        try {
            StringBuilder sql = new StringBuilder();
            sql.append("update ").append(tableName).append(" set ");
            for (int i = 0; i < length; i++) {
                String name = fields[i].getName();
                myField anno = fields[i].getAnnotation(myField.class);
                if (anno != null){
                    name = anno.field();
                    if (i == length - 1){
                        sql.append("`").append(name).append("`").append(" = ?");
                    }
                    else {
                        sql.append("`").append(name).append("`").append(" = ?,");
                    }
                }else {
                    if (i == length - 1){
                        sql.append("`").append(name).append("`").append(" = ?");
                    }
                    else {
                        sql.append("`").append(name).append("`").append(" = ?,");
                    }
                }
            }
            sql.append(" where ").append(keyName).append(" = ").append(id);
            Object[] objects = new Object[length];
            for (int i = 0; i < length; i++) {
                String name = fields[i].getName();
                String methodName = "get" + name.substring(0,1).toUpperCase() + name.substring(1);
                Method method = aClass.getDeclaredMethod(methodName);
                Object obj = method.invoke(t);
                objects[i] = obj;
            }
            a = jdbcTemplate.update(sql.toString(),objects);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return a > 0;
    }

    //查询指定记录
    public T findById(Object id){
        T t = null;
        try {
            String sql = "select * from "+ tableName +" where " + keyName + " = ?";
            t = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(tClass), id);
        }catch (Exception e){
            e.printStackTrace();
        }
        return t;
    }
}

